package com.cgw.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @类描述 View指针控件
 * @作者 YF
 * @创建时间 2020/5/15 9:36
 */
public class PointerView extends View {
    /**
     * 上下文对象
     * */
    private Context context;

    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;

    /**
     * 圆点画笔
     * */
    private Paint mPointPaint;
    /**
     * 线条画笔
     * */
    private Paint mLinePaint;

    /**
     * 路径画笔
     * */
    private Paint mPathPaint;

    /**
     * 圆点大小
     * */
    private float pointSize = 40f;

    private RectF pointRectF = new RectF();

    /**
     * 贝塞尔曲线path
     * */
    private Path path;

    public PointerView(Context context) {
        this(context,null);
    }

    public PointerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PointerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }
    /**
     * 初始化view
     * */
    private void init() {
        mPointPaint = new Paint();
        mPointPaint.setColor(Color.WHITE);
        mPointPaint.setAntiAlias(true);

        mLinePaint = new Paint();
        mLinePaint.setColor(Color.RED);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(3f);

        mPathPaint = new Paint();
        mPathPaint.setColor(Color.WHITE);
        mPathPaint.setAntiAlias(true);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
        pointRectF = new RectF(width/2-pointSize,height-2*pointSize,width/2+pointSize,height);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,100);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.GRAY);

        path = new Path();
        path.moveTo(0,0);
        path.lineTo(0,pointRectF.top+pointSize*0.8f);
        path.lineTo(pointRectF.centerX()-2.6f*pointSize,pointRectF.top+pointSize*0.8f);

        path.cubicTo(pointRectF.centerX()-1.3f*pointSize,pointRectF.top+pointSize*0.8f,
                pointRectF.centerX()-1.3f*pointSize,pointRectF.top-pointSize/2,
                pointRectF.centerX(), pointRectF.top-pointSize/2);


        path.cubicTo(pointRectF.centerX()+1.3f*pointSize,pointRectF.top-pointSize/2,
                pointRectF.centerX()+1.3f*pointSize,pointRectF.top+pointSize*0.8f,
                pointRectF.centerX()+2.6f*pointSize,pointRectF.top+pointSize*0.8f);



        path.lineTo(width,pointRectF.top+pointSize*0.8f);
        path.lineTo(width,0);
        path.close();
        canvas.drawPath(path,mPathPaint);

        drawVerticalLine(canvas);
        drawPoint(canvas);
    }



    /**
     * 画圆点
     * */
    private void drawPoint(Canvas canvas) {
        canvas.drawCircle(pointRectF.centerX(),pointRectF.centerY(),pointSize,mPointPaint);
    }
    /**
     * 画竖线
     * */
    private void drawVerticalLine(Canvas canvas) {
        canvas.drawLine(pointRectF.centerX(),0,pointRectF.centerX(),height-pointSize,mLinePaint);
    }
    float startX = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                float x = event.getX();
                float y = event.getY();
                if(pointRectF.contains(x,y)){
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX();
                float diff= moveX-startX;
                startX = moveX;
                pointRectF.offset(diff,0);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                startX = 0;
                break;
        }
        return super.onTouchEvent(event);
    }
}
